<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-row">
        <div class="filter-item">
            <span class="filter-title">场所名称：</span>
            <el-input placeholder="请输入场所名称"   size="small" class="search-item" v-model="searchObj.search1"/>
        </div>
        <div class="filter-item">
          <span class="filter-title">所属区域：</span>
            <el-cascader
            size="small"
            class="search-item"
            :props="props"
            @change="handleChange"
            v-model="searchObj.areaIds"
            style="width: 300px"
          ></el-cascader>
        </div>
        <div class="filter-item">
          <span class="filter-title">申领时间：</span>
          <!-- <el-date-picker
            size="small"
            :range-separator="null"
            start-placeholder="请选择时间"
            class="search-item date-range-item"
            format="yyyy-MM-dd"
            type="datetimerange"
            v-model="searchObj.timeRange"
            value-format="yyyy-MM-dd"
          /> -->
          <el-date-picker
            size="small"
            class="search-item date-range-item"
            v-model="searchObj.timeRange"
            type="datetimerange"
            format="yyyy-MM-dd  HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :range-separator="null"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </div>
        <div class="filter-item">
           <el-button size="small" @click="search" class=""  type="primary">
              查询
            </el-button>
            <el-button size="small" @click="reset" class="" >
              重置
            </el-button>
        </div>
      </div>
    </div>
    <el-table
      size="medium"
      :data="tabItems"
      ref="table"
      style="width: 100%;"
      v-loading="tabLoading"
    >
      <el-table-column label="场所码编码" align="center"
        prop="test1"
      />
      <el-table-column label="场所名称" align="center"
        prop="test2"
      />
      <el-table-column label="场所类型" align="center"
        prop="test3"
      />
      <el-table-column label="状态" align="center"
        prop="test4"
      />
      <el-table-column label="今日扫码次数" align="center"
        prop="test5"
      />
      <el-table-column label="累计扫码次数" align="center"
        prop="test6"
      />
      <el-table-column label="扫码频率(次/min)" align="center"
        prop="test7"
      />
      <el-table-column label="扫码峰值(次/min)" align="center"
        prop="test8"
      />
      <el-table-column label="重点人扫码次数" align="center"
        prop="test9"
      />
      <el-table-column label="错误扫码次数" align="center"
        prop="test10"
      />
      <el-table-column label="预警次数" align="center"
        prop="test11"
      />
      <el-table-column label="预警未受理" align="center"
        prop="test12"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="itemMoniDetail(scope.row)">查看监控</el-button>
          <el-button
            size="mini"
            type="text"
            @click="itemWarningDetail(scope.row)">查看预警</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-box">
      <el-pagination
        :background="true"
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-count="pagination.pageCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {reqUrl1, commAreaUrl} from '@/http/apiUrls.js';
export default {
  name: 'CodeMoni',
  data() {
    return {
      tabLoading: false,
      tableKey: 0,
      pageItems: 10, // 每页查询条数
      pagination: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        pageCount: 1, // 总页数
      },
      tabItems: [
        {test1: '20220414010',
        test2: '公司前台',
        test3: '办公场所',
        test4: '启用',
        test5: '80',
        test6: '800',
        test7: '100',
        test8: '10000',
        test9: '40',
        test10: '10',
        test11: '4',
        test12: '0'}
      ],
      searchObj: {
        search1: '',
        areaIds: [],
        timeRange: []
      },
      props: {
        lazy: true,
        checkStrictly: true,
        lazyLoad: (node, resolve) => {
          const { level } = node;
          console.log('level',level);
          const nodes = [];          
          let type = level == 0 ? "520000000000" : node.value;
          this.provinceFn(type)
            .then((res) => {
              // 节点数组
              res.map((item) => {
                let obj = {
                  value: item.code,
                  label: item.fullName,
                  leaf: node.level >= 3, 
                };
                nodes.push(obj);
              });
              // resolve 节点返回
              resolve(nodes);
            })
            .catch((error) => {
              console.log(error);
            });
        }
      }
    }
  },
  created(){
    // this.getData(1, this.pageItems, {}, {})
  },
  methods: {
    search(){
      console.log(this.searchObj);
      this.getData(1, this.pageItems, this.getSearchObj(), this.getExtraObj())
    },
    getSearchObj(){
      let obj = this.searchObj;
      const ids = obj.areaIds;
      if(ids && ids.length > 0){
        obj.areaCode = ids[ids.length-1]
      }
      return obj;
    },
    reset(){
      this.searchObj = {
        search1: '',
        areaIds:[],
        timeRange: []
      }
      //  this.getData(1, this.pageItems, {}, {})
    },
    getArea(code){
      return new Promise((resolve, reject) => {
        const requestData = {
          parentCode: code
        }
        this.$postJson(commAreaUrl, requestData).then(res => {
          if(res){
            resolve(res)
          }
        })
      })
    },
    // 获取省市区街道
    provinceFn(code) {
      // 此处是一个promise 直接返回
      return this.getArea(code);
    },
     // 监听数据变化
    handleChange(value) {
      console.log(value);
    },
    getExtraObj(){
      let obj = this.searchObj;
      let extra = {};
      if(obj.timeRange && obj.timeRange.length == 2 ){
        extra.createTime_ge = obj.timeRange[0]
        extra.createTime_le = obj.timeRange[1]
      }
      return extra
    },
    getData(current , size, model,extra){
      let requestData = {
        current,
        size,
        model,
        extra
      };
      console.log(requestData);
      this.tabLoading = true;
      this.$postJson(reqUrl1, requestData).then(res => {
        this.tabLoading = false;
        if(res){
          const pagination = {
            currentPage: parseInt(res.current), // 当前页面号
            pageSize: parseInt(res.size), // 每页条目数
            pageCount: parseInt(res.pages), // 总页面数
          }
          this.pagination = pagination;
          this.tabItems = res.records;
        }
      })
    
    },
    async handleCurrentChange(val) {
      await this.getData(val, this.pageItems, this.getSearchObj(), this.getExtraObj())
    },
    async handleSizeChange(val) {
      this.pageItems = val
      await this.getData(1, this.pageItems, this.getSearchObj(), this.getExtraObj())
    },
    itemMoniDetail(item){
      console.log(item);
      this.$router.push({
        path: "/csm/codeMoniDetail",
        query: {id: item.id}
      })
    },
    itemWarningDetail(item){
      console.log(item);
      this.$router.push({
        path: "/csm/codeWarningDetail",
        query: {id: item.id}
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .filter-row {
    width: 100%;
    margin-bottom: 8px;
    display: flex;
    .filter-item {
      width: fit-content;
      .filter-title {
        display:inline-block;
        width:100px;
        text-align:right;
        margin-left: 4px;
      }
    }
  }
}
</style>